﻿namespace YidanSoft.Library.EditorUtility.CommandBars
{
    using System;
    using System.Drawing;
    using System.Drawing.Text;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.Collections;
    using YidanSoft.Library.EditorUtility.General;
    using YidanSoft.Library.EditorUtility.Win32;
    using YidanSoft.Library.EditorUtility.WinControls;

    public class ToolBarEx : Control, IChevron
    {
        private YidanSoft.Library.EditorUtility.CommandBars.BarType barType;
        private bool bGotIsCommonCtrl6;
        private ChevronMenu chevronMenu;
        private bool doKeyboardSelect;
        private const int DROWPDOWN_ARROW_WIDTH = 14;
        private ToolBarItem[] handledItems;
        private bool[] handledItemsVisible;
        private IntPtr hookHandle;
        private ImageList imageList;
        private bool isCommonCtrl6;
        private ToolBarItemCollection items;
        private Point lastMousePosition;
        private State lastState;
        private const int MARGIN = 3;
        private const int MENUTEXT_MARGIN = 8;
        private State state;
        private bool trackEscapePressed;
        private int trackHotItem;
        private int trackNextItem;
        private bool useNewRow;

        public ToolBarEx()
        {
            this.items = new ToolBarItemCollection();
            this.handledItems = new ToolBarItem[0];
            this.handledItemsVisible = new bool[0];
            this.chevronMenu = new ChevronMenu();
            this.barType = YidanSoft.Library.EditorUtility.CommandBars.BarType.ToolBar;
            this.imageList = null;
            this.bGotIsCommonCtrl6 = false;
            this.isCommonCtrl6 = false;
            this.state = State.None;
            this.lastState = State.None;
            this.lastMousePosition = new Point(0, 0);
            this.trackHotItem = -1;
            this.trackNextItem = -1;
            this.trackEscapePressed = false;
            this.hookHandle = IntPtr.Zero;
            this.doKeyboardSelect = false;
            this.useNewRow = true;
            this.InitializeToolBar();
        }

        public ToolBarEx(bool useNewRow)
        {
            this.items = new ToolBarItemCollection();
            this.handledItems = new ToolBarItem[0];
            this.handledItemsVisible = new bool[0];
            this.chevronMenu = new ChevronMenu();
            this.barType = YidanSoft.Library.EditorUtility.CommandBars.BarType.ToolBar;
            this.imageList = null;
            this.bGotIsCommonCtrl6 = false;
            this.isCommonCtrl6 = false;
            this.state = State.None;
            this.lastState = State.None;
            this.lastMousePosition = new Point(0, 0);
            this.trackHotItem = -1;
            this.trackNextItem = -1;
            this.trackEscapePressed = false;
            this.hookHandle = IntPtr.Zero;
            this.doKeyboardSelect = false;
            this.useNewRow = true;
            this.InitializeToolBar();
            this.useNewRow = useNewRow;
        }

        public ToolBarEx(YidanSoft.Library.EditorUtility.CommandBars.BarType type)
        {
            this.items = new ToolBarItemCollection();
            this.handledItems = new ToolBarItem[0];
            this.handledItemsVisible = new bool[0];
            this.chevronMenu = new ChevronMenu();
            this.barType = YidanSoft.Library.EditorUtility.CommandBars.BarType.ToolBar;
            this.imageList = null;
            this.bGotIsCommonCtrl6 = false;
            this.isCommonCtrl6 = false;
            this.state = State.None;
            this.lastState = State.None;
            this.lastMousePosition = new Point(0, 0);
            this.trackHotItem = -1;
            this.trackNextItem = -1;
            this.trackEscapePressed = false;
            this.hookHandle = IntPtr.Zero;
            this.doKeyboardSelect = false;
            this.useNewRow = true;
            this.barType = type;
            this.InitializeToolBar();
        }

        public ToolBarEx(YidanSoft.Library.EditorUtility.CommandBars.BarType type, bool useNewRow)
        {
            this.items = new ToolBarItemCollection();
            this.handledItems = new ToolBarItem[0];
            this.handledItemsVisible = new bool[0];
            this.chevronMenu = new ChevronMenu();
            this.barType = YidanSoft.Library.EditorUtility.CommandBars.BarType.ToolBar;
            this.imageList = null;
            this.bGotIsCommonCtrl6 = false;
            this.isCommonCtrl6 = false;
            this.state = State.None;
            this.lastState = State.None;
            this.lastMousePosition = new Point(0, 0);
            this.trackHotItem = -1;
            this.trackNextItem = -1;
            this.trackEscapePressed = false;
            this.hookHandle = IntPtr.Zero;
            this.doKeyboardSelect = false;
            this.useNewRow = true;
            this.barType = type;
            this.InitializeToolBar();
            this.useNewRow = useNewRow;
        }

        private void Attach()
        {
            this.items.Changed += new EventHandler(this.Items_Changed);
            int count = this.Items.Count;
            this.handledItems = new ToolBarItem[count];
            this.handledItemsVisible = new bool[count];
            for (int i = 0; i < count; i++)
            {
                ToolBarItem item = this.Items[i];
                item.Changed += new EventHandler(this.Item_Changed);
                this.handledItems[i] = item;
                this.handledItemsVisible[i] = item.Visible;
            }
        }

        private void BeginUpdate()
        {
            WindowsAPI.SendMessage(base.Handle, 11, 0, 0);
        }

        protected override void CreateHandle()
        {
            if (!base.RecreatingHandle)
            {
                YidanSoft.Library.EditorUtility.Win32.INITCOMMONCONTROLSEX icc = new YidanSoft.Library.EditorUtility.Win32.INITCOMMONCONTROLSEX();
                icc.dwSize = Marshal.SizeOf(typeof(YidanSoft.Library.EditorUtility.Win32.INITCOMMONCONTROLSEX));
                icc.dwICC = 0x404;
                WindowsAPI.InitCommonControlsEx(icc);
            }
            base.CreateHandle();
        }

        private void Detach()
        {
            foreach (ToolBarItem item in this.handledItems)
            {
                item.Changed -= new EventHandler(this.Item_Changed);
            }
            this.handledItems = null;
            this.handledItemsVisible = null;
            this.items.Changed -= new EventHandler(this.Items_Changed);
        }

        protected void DrawArrowGlyph(Graphics g, Rectangle rectangle)
        {
            Point[] points = new Point[3];
            int num = rectangle.Left + ((rectangle.Width - 14) + 1);
            int y = (rectangle.Top + (rectangle.Height / 2)) - 1;
            points[0] = new Point(num + 4, y);
            points[1] = new Point(num + 9, y);
            points[2] = new Point(num + 6, y + 3);
            g.FillPolygon(Brushes.Black, points);
        }

        private bool DropDownFilter(ref Message message)
        {
            if (this.state != State.HotTracking)
            {
                throw new Exception();
            }
            this.SetHotItem(this.trackHotItem);
            if (message.Msg == 0x100)
            {
                Keys keys = ((Keys) ((int) message.WParam)) | Control.ModifierKeys;
                switch (keys)
                {
                    case Keys.Left:
                    case Keys.Right:
                        this.doKeyboardSelect = true;
                        break;

                    case Keys.Left:
                        this.TrackDropDownNext(this.GetPreviousItem(this.trackHotItem));
                        return true;
                }
                ToolBarItem item = this.items[this.trackHotItem];
                if ((keys == Keys.Right) && ((item.ToolBarItemMenu.SelectedMenuItem == null) || (item.ToolBarItemMenu.SelectedMenuItem.MenuItems.Count == 0)))
                {
                    this.TrackDropDownNext(this.GetNextItem(this.trackHotItem));
                    return true;
                }
                if (keys == Keys.Escape)
                {
                    this.trackEscapePressed = true;
                }
            }
            else if ((message.Msg == 0x200) || (message.Msg == 0x201))
            {
                Point p = new Point(((int) message.LParam) & 0xffff, ((int) message.LParam) >> 0x10);
                p = base.PointToClient(p);
                if (message.Msg == 0x200)
                {
                    if (p != this.lastMousePosition)
                    {
                        int index = this.HitTest(p);
                        if (this.IsValid(index) && (index != this.trackHotItem))
                        {
                            this.TrackDropDownNext(index);
                        }
                        this.lastMousePosition = p;
                    }
                }
                else if ((message.Msg == 0x201) && (this.HitTest(p) == this.trackHotItem))
                {
                    this.TrackDropDownNext(-1);
                    return true;
                }
            }
            return false;
        }

        private IntPtr DropDownHook(int code, IntPtr wparam, IntPtr lparam)
        {
            if (code == 2)
            {
                YidanSoft.Library.EditorUtility.Win32.MSG msg = (YidanSoft.Library.EditorUtility.Win32.MSG) Marshal.PtrToStructure(lparam, typeof(YidanSoft.Library.EditorUtility.Win32.MSG));
                Message message = Message.Create(msg.hwnd, msg.message, msg.wParam, msg.lParam);
                if (this.DropDownFilter(ref message))
                {
                    return (IntPtr) 1;
                }
            }
            return WindowsAPI.CallNextHookEx(this.hookHandle, code, wparam, lparam);
        }

        private void EndUpdate()
        {
            WindowsAPI.SendMessage(base.Handle, 11, 1, 0);
        }

        ~ToolBarEx()
        {
            this.Detach();
        }

        private MenuItem FindShortcutItem(Menu.MenuItemCollection collection, Keys keys)
        {
            int count = collection.Count;
            foreach (MenuItem item in collection)
            {
                if (((item.Shortcut == ((Shortcut) ((int) keys))) && item.Enabled) && item.Visible)
                {
                    return item;
                }
                return this.FindShortcutItem(item.MenuItems, keys);
            }
            return null;
        }

        private MenuItem FindShortcutItem(MenuItem[] menuItems, Keys keys)
        {
            MenuItem item = null;
            foreach (MenuItem item2 in menuItems)
            {
                if (((item2.Shortcut == ((Shortcut) ((int) keys))) && item2.Enabled) && item2.Visible)
                {
                    return item2;
                }
                item = this.FindShortcutItem(item2.MenuItems, keys);
                if (item != null)
                {
                    return item;
                }
            }
            return item;
        }

        private YidanSoft.Library.EditorUtility.Win32.TBBUTTONINFO GetButtonInfo(int index)
        {
            ToolBarItem item = this.items[index];
            YidanSoft.Library.EditorUtility.Win32.TBBUTTONINFO tbbuttoninfo = new YidanSoft.Library.EditorUtility.Win32.TBBUTTONINFO();
            tbbuttoninfo.cbSize = Marshal.SizeOf(typeof(YidanSoft.Library.EditorUtility.Win32.TBBUTTONINFO));
            tbbuttoninfo.dwMask = 0x6d;
            tbbuttoninfo.idCommand = index;
            tbbuttoninfo.iImage = -1;
            tbbuttoninfo.fsState = 0;
            tbbuttoninfo.cx = 0;
            tbbuttoninfo.lParam = IntPtr.Zero;
            tbbuttoninfo.pszText = IntPtr.Zero;
            tbbuttoninfo.cchText = 0;
            if (item.Style == ToolBarItemStyle.ComboBox)
            {
                tbbuttoninfo.fsStyle = 0;
                tbbuttoninfo.cx = (short) item.ComboBox.Width;
                WindowsAPI.SetParent(item.ComboBox.Handle, base.Handle);
            }
            else if ((item.Text != null) && (item.Text != string.Empty))
            {
                tbbuttoninfo.fsStyle = 0;
                tbbuttoninfo.cx = 3;
                if (item.Image != null)
                {
                    tbbuttoninfo.cx = (short) (tbbuttoninfo.cx + ((short) (item.Image.Size.Width + 3)));
                }
                if (item.Style == ToolBarItemStyle.DropDownButton)
                {
                    tbbuttoninfo.cx = (short) (tbbuttoninfo.cx + 14);
                }
                Graphics graphics = base.CreateGraphics();
                string text = item.Text;
                int startIndex = text.IndexOf('&');
                if (startIndex != -1)
                {
                    text = item.Text.Remove(startIndex, 1);
                }
                Size size = TextUtil.GetTextSize(graphics, text, SystemInformation.MenuFont);
                graphics.Dispose();
                if (this.barType == YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar)
                {
                    tbbuttoninfo.cx = (short) (tbbuttoninfo.cx + ((short) (size.Width + 0x10)));
                }
                else
                {
                    tbbuttoninfo.cx = (short) (tbbuttoninfo.cx + ((short) (size.Width + 6)));
                }
                tbbuttoninfo.dwMask |= 2;
                tbbuttoninfo.pszText = Marshal.StringToHGlobalAuto(item.Text + "\0");
                tbbuttoninfo.cchText = item.Text.Length;
                if (this.IsCommonCtrl6() && (this.barType != YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar))
                {
                    tbbuttoninfo.cx = (short) (tbbuttoninfo.cx + 6);
                }
            }
            else
            {
                tbbuttoninfo.fsStyle = 0x10;
                tbbuttoninfo.cx = 0;
            }
            if (!item.Visible)
            {
                tbbuttoninfo.fsState = (byte) (tbbuttoninfo.fsState | 8);
            }
            if (item.Style == ToolBarItemStyle.Separator)
            {
                tbbuttoninfo.fsStyle = (byte) (tbbuttoninfo.fsStyle | 1);
            }
            else
            {
                if (item.Enabled)
                {
                    tbbuttoninfo.fsState = (byte) (tbbuttoninfo.fsState | 4);
                }
                if (item.Style == ToolBarItemStyle.DropDownButton)
                {
                    tbbuttoninfo.fsStyle = (byte) (tbbuttoninfo.fsStyle | 8);
                }
                if ((item.Style == ToolBarItemStyle.PushButton) && item.Checked)
                {
                    tbbuttoninfo.fsState = (byte) (tbbuttoninfo.fsState | 1);
                }
            }
            if (item.Style == ToolBarItemStyle.Separator)
            {
                tbbuttoninfo.iImage = -2;
                return tbbuttoninfo;
            }
            if (item.Image != null)
            {
                tbbuttoninfo.iImage = index;
            }
            return tbbuttoninfo;
        }

        private int GetHotItemIndex()
        {
            return WindowsAPI.SendMessage(base.Handle, 0x447, 0, 0);
        }

        public int GetIdealSize()
        {
            Size size = new Size(0, 0);
            for (int i = 0; i < this.items.Count; i++)
            {
                YidanSoft.Library.EditorUtility.Win32.RECT lParam = new YidanSoft.Library.EditorUtility.Win32.RECT();
                WindowsAPI.SendMessage(base.Handle, 0x433, i, ref lParam);
                int num2 = lParam.bottom - lParam.top;
                if (num2 > size.Height)
                {
                    size.Height = num2;
                }
                size.Width += lParam.right - lParam.left;
            }
            return size.Width;
        }

        private int GetNextItem(int index)
        {
            if (index == -1)
            {
                throw new Exception();
            }
            int num = WindowsAPI.SendMessage(base.Handle, 0x418, 0, 0);
            index++;
            if (index >= num)
            {
                index = 0;
            }
            return index;
        }

        private int GetPreviousItem(int index)
        {
            if (index == -1)
            {
                throw new Exception();
            }
            int num = WindowsAPI.SendMessage(base.Handle, 0x418, 0, 0);
            index--;
            if (index < 0)
            {
                index = num - 1;
            }
            return index;
        }

        private bool HasText()
        {
            for (int i = 0; i < this.items.Count; i++)
            {
                if ((this.items[i].Text != null) && (this.items[i].Text != string.Empty))
                {
                    return true;
                }
            }
            return false;
        }

        private int HitTest(Point point)
        {
            YidanSoft.Library.EditorUtility.Win32.POINT lParam = new YidanSoft.Library.EditorUtility.Win32.POINT();
            lParam.x = point.X;
            lParam.y = point.Y;
            int num = WindowsAPI.SendMessage(base.Handle, 0x445, 0, ref lParam);
            if (num > 0)
            {
                point = base.PointToScreen(point);
                if (!base.RectangleToScreen(new Rectangle(0, 0, base.Width, base.Height)).Contains(point))
                {
                    return -1;
                }
            }
            return num;
        }

        private void InitializeToolBar()
        {
            base.SetStyle(ControlStyles.UserPaint, false);
            base.TabStop = false;
            this.Dock = DockStyle.Top;
            this.Attach();
        }

        private bool IsCommonCtrl6()
        {
            if (!this.bGotIsCommonCtrl6)
            {
                YidanSoft.Library.EditorUtility.Win32.DLLVERSIONINFO dvi = new YidanSoft.Library.EditorUtility.Win32.DLLVERSIONINFO();
                dvi.cbSize = Marshal.SizeOf(typeof(YidanSoft.Library.EditorUtility.Win32.DLLVERSIONINFO));
                WindowsAPI.GetCommonControlDLLVersion(ref dvi);
                this.bGotIsCommonCtrl6 = true;
                this.isCommonCtrl6 = dvi.dwMajorVersion >= 6;
            }
            return this.isCommonCtrl6;
        }

        private bool IsValid(int index)
        {
            int num = WindowsAPI.SendMessage(base.Handle, 0x418, 0, 0);
            return ((index >= 0) && (index < num));
        }

        private void Item_Changed(object s, EventArgs e)
        {
            ToolBarItem[] handledItems = this.handledItems;
            foreach (ToolBarItem item in handledItems)
            {
                if (item == s)
                {
                    if (item == null)
                    {
                        break;
                    }
                    int index = this.items.IndexOf(item);
                    if (index == -1)
                    {
                        break;
                    }
                    this.UpdateItem(index);
                }
            }
        }

        private void Items_Changed(object s, EventArgs e)
        {
            this.UpdateItems();
        }

        private void MenuChar(ref Message message)
        {
            ToolBarItem item = this.items[this.trackHotItem];
            Menu toolBarItemMenu = item.ToolBarItemMenu.FindMenuItem(0, message.LParam);
            if (item.ToolBarItemMenu.Handle == message.LParam)
            {
                toolBarItemMenu = item.ToolBarItemMenu;
            }
            if (toolBarItemMenu != null)
            {
                char ch = char.ToUpper((char) (((int) message.WParam) & 0xffff));
                int num = 0;
                foreach (MenuItem item2 in toolBarItemMenu.MenuItems)
                {
                    if (((item2 != null) && item2.OwnerDraw) && (item2.Mnemonic == ch))
                    {
                        message.Result = (IntPtr) (0x20000 | num);
                        break;
                    }
                    if (item2.Visible)
                    {
                        num++;
                    }
                }
            }
        }

        private void NotifyCustomDraw(ref Message m)
        {
            m.Result = (IntPtr) 0L;
            NMTBCUSTOMDRAW lParam = (NMTBCUSTOMDRAW) m.GetLParam(typeof(NMTBCUSTOMDRAW));
            switch (lParam.nmcd.dwDrawStage)
            {
                case 1:
                    m.Result = (IntPtr) 0x20L;
                    break;

                case 0x10001:
                    this.NotifyCustomDrawToolBar(ref m);
                    break;
            }
        }

        private void NotifyCustomDrawToolBar(ref Message m)
        {
            m.Result = (IntPtr) 0L;
            if (!this.IsCommonCtrl6())
            {
                NMTBCUSTOMDRAW lParam = (NMTBCUSTOMDRAW) m.GetLParam(typeof(NMTBCUSTOMDRAW));
                YidanSoft.Library.EditorUtility.Win32.RECT rc = lParam.nmcd.rc;
                Rectangle rect = new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
                Graphics graphics = Graphics.FromHdc(lParam.nmcd.hdc);
                int dwItemSpec = lParam.nmcd.dwItemSpec;
                ToolBarItem item = this.items[dwItemSpec];
                if (item.Style == ToolBarItemStyle.ComboBox)
                {
                    this.UpdateComboBoxPosition(dwItemSpec);
                    m.Result = (IntPtr) 4L;
                }
                else
                {
                    Point mousePosition;
                    bool flag = (lParam.nmcd.uItemState & 0x40) != 0;
                    bool flag2 = (lParam.nmcd.uItemState & 1) != 0;
                    bool flag3 = (lParam.nmcd.uItemState & 4) != 0;
                    string text = item.Text;
                    bool flag4 = (text != string.Empty) && (text != null);
                    if (item.Checked)
                    {
                        if (flag)
                        {
                            graphics.FillRectangle(new SolidBrush(ColorUtil.VSNetPressedColor), rect);
                        }
                        else
                        {
                            graphics.FillRectangle(new SolidBrush(ColorUtil.VSNetCheckedColor), rect);
                        }
                        graphics.DrawRectangle(new Pen(new SolidBrush(ColorUtil.VSNetBorderColor), 1f), rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                    }
                    else if (flag2)
                    {
                        if (item.Style == ToolBarItemStyle.DropDownButton)
                        {
                            graphics.FillRectangle(new SolidBrush(ColorUtil.VSNetSelectionColor), rect);
                            graphics.FillRectangle(new SolidBrush(ColorUtil.VSNetPressedColor), rect.Left, rect.Top, (rect.Width - 14) + 1, rect.Height);
                            graphics.DrawRectangle(new Pen(new SolidBrush(ColorUtil.VSNetBorderColor), 1f), rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                        }
                        else if (this.barType == YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar)
                        {
                            graphics.FillRectangle(new SolidBrush(ColorUtil.VSNetControlColor), rect);
                            if (ColorUtil.UsingCustomColor)
                            {
                                graphics.DrawRectangle(new Pen(ColorUtil.VSNetBorderColor), rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                            }
                            else
                            {
                                ControlPaint.DrawBorder3D(graphics, rect.Left, rect.Top, rect.Width - 1, rect.Height - 1, Border3DStyle.Flat, Border3DSide.Right | Border3DSide.Top | Border3DSide.Left);
                            }
                        }
                        else
                        {
                            graphics.FillRectangle(new SolidBrush(ColorUtil.VSNetPressedColor), rect);
                            graphics.DrawRectangle(new Pen(new SolidBrush(ColorUtil.VSNetBorderColor), 1f), rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                        }
                    }
                    else if ((item.Style == ToolBarItemStyle.DropDownButton) && item.Dropped)
                    {
                        graphics.FillRectangle(new SolidBrush(ColorUtil.VSNetControlColor), rect);
                        graphics.DrawRectangle(new Pen(new SolidBrush(SystemColors.ControlDark), 1f), rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                    }
                    else if (flag)
                    {
                        graphics.FillRectangle(new SolidBrush(ColorUtil.VSNetSelectionColor), rect);
                        graphics.DrawRectangle(new Pen(new SolidBrush(ColorUtil.VSNetBorderColor), 1f), rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                    }
                    else if (item.Style == ToolBarItemStyle.DropDownButton)
                    {
                        IntPtr parent = WindowsAPI.GetParent(base.Handle);
                        IntPtr zero = IntPtr.Zero;
                        bool flag5 = false;
                        if (parent != IntPtr.Zero)
                        {
                            zero = WindowsAPI.GetParent(parent);
                            if (zero != IntPtr.Zero)
                            {
                                flag5 = zero == WindowsAPI.GetFocus();
                            }
                        }
                        if ((zero != IntPtr.Zero) && flag5)
                        {
                            mousePosition = Control.MousePosition;
                            Point pt = base.PointToClient(mousePosition);
                            if (rect.Contains(pt))
                            {
                                graphics.FillRectangle(new SolidBrush(ColorUtil.VSNetSelectionColor), rect);
                                graphics.DrawRectangle(new Pen(new SolidBrush(ColorUtil.VSNetBorderColor), 1f), rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                                rc.right -= 14;
                                graphics.DrawLine(new Pen(new SolidBrush(ColorUtil.VSNetBorderColor), 1f), rc.right + 1, rc.top, rc.right + 1, rc.top + (rc.bottom - rc.top));
                                rc.right += 14;
                            }
                        }
                    }
                    if (item.Style == ToolBarItemStyle.DropDownButton)
                    {
                        this.DrawArrowGlyph(graphics, rect);
                        rc.right -= 14;
                        if (!(!flag || item.Dropped))
                        {
                            graphics.DrawLine(new Pen(new SolidBrush(ColorUtil.VSNetBorderColor), 1f), rc.right + 1, rc.top, rc.right + 1, rc.top + (rc.bottom - rc.top));
                        }
                        item.Dropped = false;
                    }
                    Image image = item.Image;
                    if (image != null)
                    {
                        Size size = image.Size;
                        Point point3 = new Point(rc.left + (((rc.right - rc.left) - size.Width) / 2), rc.top + (((rc.bottom - rc.top) - size.Height) / 2));
                        if (flag4)
                        {
                            point3.X = rc.left + 3;
                        }
                        if (flag3)
                        {
                            ControlPaint.DrawImageDisabled(graphics, image, point3.X, point3.Y, ColorUtil.VSNetSelectionColor);
                        }
                        else if (!((!flag || flag2) || item.Checked))
                        {
                            ControlPaint.DrawImageDisabled(graphics, image, point3.X + 1, point3.Y, ColorUtil.VSNetSelectionColor);
                            graphics.DrawImage(image, point3.X, point3.Y - 1);
                        }
                        else
                        {
                            if (item.Checked && !flag2)
                            {
                                point3.Y--;
                            }
                            graphics.DrawImage(image, point3.X, point3.Y);
                        }
                    }
                    if (flag4)
                    {
                        string str2 = item.Text;
                        int index = str2.IndexOf('&');
                        if ((this.barType == YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar) && (index != -1))
                        {
                            str2 = item.Text.Remove(index, 1);
                        }
                        Size size2 = TextUtil.GetTextSize(graphics, str2, SystemInformation.MenuFont);
                        if (this.barType == YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar)
                        {
                            int x = rc.left + (((rc.right - rc.left) - size2.Width) / 2);
                            mousePosition = new Point(x, rc.top + (((rc.bottom - rc.top) - size2.Height) / 2));
                        }
                        else
                        {
                            mousePosition = new Point(rc.left, rc.top + (((rc.bottom - rc.top) - size2.Height) / 2));
                            if (image != null)
                            {
                                mousePosition.X = ((rc.left + 3) + image.Size.Width) + 3;
                            }
                        }
                        StringFormat format = new StringFormat();
                        format.HotkeyPrefix = HotkeyPrefix.Show;
                        graphics.DrawString(item.Text, SystemInformation.MenuFont, Brushes.Black, (PointF) mousePosition, format);
                    }
                    m.Result = (IntPtr) 4L;
                }
            }
        }

        private void NotifyNeedTextA(ref Message m)
        {
            YidanSoft.Library.EditorUtility.Win32.TOOLTIPTEXTA lParam = (YidanSoft.Library.EditorUtility.Win32.TOOLTIPTEXTA) m.GetLParam(typeof(YidanSoft.Library.EditorUtility.Win32.TOOLTIPTEXTA));
            ToolBarItem item = this.items[lParam.hdr.idFrom];
            string toolTip = item.ToolTip;
            if ((toolTip != null) && (toolTip != string.Empty))
            {
                lParam.szText = toolTip;
                lParam.hinst = IntPtr.Zero;
                if (this.RightToLeft == RightToLeft.Yes)
                {
                    lParam.uFlags |= 4;
                }
                Marshal.StructureToPtr(lParam, m.LParam, true);
                m.Result = (IntPtr) 1;
            }
        }

        private void NotifyNeedTextW(ref Message m)
        {
            if (Marshal.SystemDefaultCharSize == 2)
            {
                YidanSoft.Library.EditorUtility.Win32.TOOLTIPTEXT lParam = (YidanSoft.Library.EditorUtility.Win32.TOOLTIPTEXT) m.GetLParam(typeof(YidanSoft.Library.EditorUtility.Win32.TOOLTIPTEXT));
                ToolBarItem item = this.items[lParam.hdr.idFrom];
                string toolTip = item.ToolTip;
                if ((toolTip != null) && (toolTip != string.Empty))
                {
                    lParam.szText = toolTip;
                    lParam.hinst = IntPtr.Zero;
                    if (this.RightToLeft == RightToLeft.Yes)
                    {
                        lParam.uFlags |= 4;
                    }
                    Marshal.StructureToPtr(lParam, m.LParam, true);
                    m.Result = (IntPtr) 1;
                }
            }
        }

        protected override void OnFontChanged(EventArgs e)
        {
            base.OnFontChanged(e);
            this.UpdateItems();
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            WindowsAPI.SendMessage(base.Handle, 0x41e, Marshal.SizeOf(typeof(YidanSoft.Library.EditorUtility.Win32.TBBUTTON)), 0);
            int lParam = 0x90;
            if (this.BarType == YidanSoft.Library.EditorUtility.CommandBars.BarType.ToolBar)
            {
                lParam |= 1;
            }
            WindowsAPI.SendMessage(base.Handle, 0x454, 0, lParam);
            this.RealizeItems();
            base.OnHandleCreated(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if ((this.barType == YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar) && ((e.Button == MouseButtons.Left) && (e.Clicks == 1)))
            {
                Point point = new Point(e.X, e.Y);
                int index = this.HitTest(point);
                if (this.IsValid(index))
                {
                    this.TrackDropDown(index);
                    return;
                }
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.barType == YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar)
            {
                Point point = new Point(e.X, e.Y);
                if (this.state == State.Hot)
                {
                    int index = this.HitTest(point);
                    if (this.IsValid(index) && (point != this.lastMousePosition))
                    {
                        this.SetHotItem(index);
                    }
                    return;
                }
                this.lastMousePosition = point;
            }
            base.OnMouseMove(e);
        }

        public override bool PreProcessMessage(ref Message message)
        {
            if ((message.Msg == 0x100) || (message.Msg == 260))
            {
                Keys keys = ((Keys) ((int) message.WParam)) | Control.ModifierKeys;
                ToolBarItem item = this.items[keys];
                if ((item != null) && item.Enabled)
                {
                    item.RaiseClick();
                    return true;
                }
                if (this.barType == YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar)
                {
                    MenuItem item2 = null;
                    foreach (ToolBarItem item3 in this.items)
                    {
                        item2 = this.FindShortcutItem(item3.MenuItems, keys);
                        if (item2 != null)
                        {
                            break;
                        }
                    }
                    if (item2 != null)
                    {
                        item2.PerformClick();
                    }
                }
                if ((keys & Keys.Alt) != Keys.None)
                {
                    Keys keys2 = keys & Keys.KeyCode;
                    char c = (char) ((ushort) keys2);
                    if (char.IsDigit(c) || char.IsLetter(c))
                    {
                        ToolBarItem item4 = this.items[c];
                        if (item4 != null)
                        {
                            if (this.barType == YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar)
                            {
                                this.TrackDropDown(item4.Index);
                            }
                            else
                            {
                                item4.RaiseClick();
                            }
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        private void RealizeItems()
        {
            this.UpdateImageList();
            for (int i = 0; i < this.items.Count; i++)
            {
                this.items[i].Index = i;
                this.items[i].ToolBar = this;
                YidanSoft.Library.EditorUtility.Win32.TBBUTTON lParam = new YidanSoft.Library.EditorUtility.Win32.TBBUTTON();
                lParam.idCommand = i;
                WindowsAPI.SendMessage(base.Handle, 0x415, i, ref lParam);
                YidanSoft.Library.EditorUtility.Win32.TBBUTTONINFO buttonInfo = this.GetButtonInfo(i);
                WindowsAPI.SendMessage(base.Handle, 0x440, i, ref buttonInfo);
                if (this.items[i].Style == ToolBarItemStyle.ComboBox)
                {
                    this.UpdateComboBoxPosition(i);
                }
            }
            this.UpdateSize();
        }

        private void SetHotItem(int index)
        {
            WindowsAPI.SendMessage(base.Handle, 0x448, index, 0);
        }

        private void SetState(State state, int index)
        {
            if (this.state != state)
            {
                if (state == State.None)
                {
                    index = -1;
                }
                this.SetHotItem(index);
                if (state == State.HotTracking)
                {
                    this.trackEscapePressed = false;
                    this.trackHotItem = index;
                }
            }
            this.lastState = this.state;
            this.state = state;
        }

        internal void ToolbarSizeChanged()
        {
            if (this.BarType != YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar)
            {
                Size clientSize = base.ClientSize;
                for (int i = 0; i < this.items.Count; i++)
                {
                    ToolBarItem item = this.items[i];
                    if (item.Style == ToolBarItemStyle.ComboBox)
                    {
                        YidanSoft.Library.EditorUtility.Win32.RECT lParam = new YidanSoft.Library.EditorUtility.Win32.RECT();
                        WindowsAPI.SendMessage(base.Handle, 0x41d, i, ref lParam);
                        if (lParam.right > clientSize.Width)
                        {
                            item.ComboBox.Visible = false;
                        }
                        else
                        {
                            item.ComboBox.Visible = true;
                        }
                    }
                }
            }
        }

        private void TrackDropDown(int index)
        {
            while (index >= 0)
            {
                this.trackNextItem = -1;
                this.BeginUpdate();
                ToolBarItem item = this.items[index];
                item.RaiseDropDown();
                WindowsAPI.SendMessage(base.Handle, 0x403, index, -1);
                if (this.doKeyboardSelect)
                {
                    WindowsAPI.PostMessage(base.Handle, 0x100, 40, 1);
                    WindowsAPI.PostMessage(base.Handle, 0x101, 40, 1);
                }
                this.doKeyboardSelect = false;
                this.SetState(State.HotTracking, index);
                WindowsAPI.HookProc proc = new WindowsAPI.HookProc(this.DropDownHook);
                GCHandle handle = GCHandle.Alloc(proc);
                this.hookHandle = WindowsAPI.SetWindowsHookEx(-1, proc, IntPtr.Zero, WindowsAPI.GetCurrentThreadId());
                if (this.hookHandle == IntPtr.Zero)
                {
                    throw new SecurityException();
                }
                YidanSoft.Library.EditorUtility.Win32.RECT lParam = new YidanSoft.Library.EditorUtility.Win32.RECT();
                WindowsAPI.SendMessage(base.Handle, 0x433, index, ref lParam);
                Point pos = new Point(lParam.left, lParam.bottom - 1);
                this.EndUpdate();
                base.Update();
                CommandBarMenu toolBarItemMenu = item.ToolBarItemMenu;
                if (toolBarItemMenu == null)
                {
                    break;
                }
                toolBarItemMenu.Show(this, pos);
                WindowsAPI.UnhookWindowsHookEx(this.hookHandle);
                handle.Free();
                this.hookHandle = IntPtr.Zero;
                WindowsAPI.SendMessage(base.Handle, 0x403, index, 0);
                this.SetState(this.trackEscapePressed ? State.Hot : State.None, index);
                index = this.trackNextItem;
            }
        }

        private void TrackDropDownNext(int index)
        {
            if (index != this.trackHotItem)
            {
                WindowsAPI.PostMessage(base.Handle, 0x1f, 0, 0);
                this.trackNextItem = index;
            }
        }

        private void UpdateComboBoxPosition(int index)
        {
            YidanSoft.Library.EditorUtility.Win32.RECT lParam = new YidanSoft.Library.EditorUtility.Win32.RECT();
            WindowsAPI.SendMessage(base.Handle, 0x433, index, ref lParam);
            int num = lParam.bottom - lParam.top;
            int height = this.Items[index].ComboBox.Bounds.Height;
            int y = lParam.top + ((num - height) / 2);
            this.Items[index].ComboBox.Bounds = new Rectangle(lParam.left + 1, y, (lParam.right - lParam.left) - 2, height);
        }

        private void UpdateImageList()
        {
            int num;
            Image image;
            Size size = new Size(0x10, SystemInformation.MenuFont.Height);
            for (num = 0; num < this.items.Count; num++)
            {
                image = this.items[num].Image;
                if (image != null)
                {
                    if (image.Width > size.Width)
                    {
                        size.Width = image.Width;
                    }
                    if (image.Height > size.Height)
                    {
                        size.Height = image.Height;
                    }
                }
            }
            this.imageList = new ImageList();
            this.imageList.ImageSize = size;
            this.imageList.ColorDepth = ColorDepth.Depth32Bit;
            for (num = 0; num < this.items.Count; num++)
            {
                if (this.items[num].Style == ToolBarItemStyle.ComboBox)
                {
                    this.items[num].ComboBox.Font = SystemInformation.MenuFont;
                    ComboBoxBase comboBox = (ComboBoxBase) this.items[num].ComboBox;
                    int num2 = 2;
                    if (SystemInformation.MenuFont.Height >= 20)
                    {
                        num2 = 5;
                    }
                    comboBox.SetFontHeight(SystemInformation.MenuFont.Height - num2);
                }
                image = this.items[num].Image;
                this.imageList.Images.Add((image != null) ? image : new Bitmap(size.Width, size.Height));
            }
            IntPtr lParam = (this.BarType == YidanSoft.Library.EditorUtility.CommandBars.BarType.MenuBar) ? IntPtr.Zero : this.imageList.Handle;
            WindowsAPI.SendMessage(base.Handle, 0x430, 0, lParam);
        }

        private void UpdateItem(int index)
        {
            if (base.IsHandleCreated)
            {
                if (this.items[index].Visible == this.handledItemsVisible[index])
                {
                    YidanSoft.Library.EditorUtility.Win32.TBBUTTONINFO buttonInfo = this.GetButtonInfo(index);
                    WindowsAPI.SendMessage(base.Handle, 0x440, index, ref buttonInfo);
                    if (this.items[index].Style == ToolBarItemStyle.ComboBox)
                    {
                        this.UpdateComboBoxPosition(index);
                    }
                }
                else
                {
                    this.UpdateItems();
                }
            }
        }

        private void UpdateItems()
        {
            this.Detach();
            this.Attach();
            if (base.IsHandleCreated)
            {
                base.RecreateHandle();
            }
        }

        private void UpdateSize()
        {
            Size size = new Size(0, 0);
            for (int i = 0; i < this.items.Count; i++)
            {
                YidanSoft.Library.EditorUtility.Win32.RECT lParam = new YidanSoft.Library.EditorUtility.Win32.RECT();
                WindowsAPI.SendMessage(base.Handle, 0x433, i, ref lParam);
                int num2 = lParam.bottom - lParam.top;
                if (num2 > size.Height)
                {
                    size.Height = num2;
                }
                size.Width += lParam.right - lParam.left;
            }
            base.Size = size;
        }

        public void UpdateToolBarItems()
        {
            this.UpdateImageList();
            for (int i = 0; i < this.items.Count; i++)
            {
                if (this.items[i].Style == ToolBarItemStyle.ComboBox)
                {
                    WindowsAPI.SetParent(this.items[i].ComboBox.Handle, IntPtr.Zero);
                    this.items[i].ComboBox.Parent = null;
                }
            }
            this.UpdateItems();
        }

        protected override void WndProc(ref Message message)
        {
            YidanSoft.Library.EditorUtility.Win32.NMHDR nmhdr;
            base.WndProc(ref message);
            int iItem = -1;
            ToolBarItem item = null;
            int msg = message.Msg;
            if (msg <= 0x4e)
            {
                if ((msg != 20) && (msg == 0x4e))
                {
                    goto Label_0098;
                }
            }
            else
            {
                switch (msg)
                {
                    case 0x120:
                        this.MenuChar(ref message);
                        return;

                    case 0x204e:
                        goto Label_0098;
                }
                if (msg == 0x2111)
                {
                    iItem = ((int) message.WParam) & 0xffff;
                    this.items[iItem].RaiseClick();
                    base.WndProc(ref message);
                    base.ResetMouseEventArgs();
                }
            }
            return;
        Label_0098:
            nmhdr = (YidanSoft.Library.EditorUtility.Win32.NMHDR) message.GetLParam(typeof(YidanSoft.Library.EditorUtility.Win32.NMHDR));
            switch (nmhdr.code)
            {
                case -530:
                    this.NotifyNeedTextW(ref message);
                    return;

                case -520:
                    this.NotifyNeedTextA(ref message);
                    return;

                case -12:
                    this.NotifyCustomDraw(ref message);
                    return;

                case -713:
                    return;

                case -710:
                {
                    YidanSoft.Library.EditorUtility.Win32.NMTOOLBAR lParam = (YidanSoft.Library.EditorUtility.Win32.NMTOOLBAR) message.GetLParam(typeof(YidanSoft.Library.EditorUtility.Win32.NMTOOLBAR));
                    iItem = lParam.iItem;
                    item = this.items[iItem];
                    item.Dropped = true;
                    item.RaiseDropDown();
                    return;
                }
                case -706:
                    message.Result = (IntPtr) 1;
                    return;
            }
        }

        void IChevron.Show(Control control, Point point)
        {
            ToolBarItem item;
            int num2;
            ToolBarItem item2;
            ToolBarItemCollection items = new ToolBarItemCollection();
            Size clientSize = base.ClientSize;
            int num = 0;
            bool flag = true;
            bool flag2 = false;
            for (num2 = 0; num2 < this.items.Count; num2++)
            {
                bool flag3 = false;
                YidanSoft.Library.EditorUtility.Win32.RECT lParam = new YidanSoft.Library.EditorUtility.Win32.RECT();
                WindowsAPI.SendMessage(base.Handle, 0x41d, num2, ref lParam);
                if (lParam.right > clientSize.Width)
                {
                    item2 = this.items[num2];
                    if (item2.ComboBox != null)
                    {
                        flag2 = true;
                    }
                    flag3 = item2.Style == ToolBarItemStyle.Separator;
                    if (item2.Visible && (!flag3 || (items.Count != 0)))
                    {
                        num = items.Count;
                        if (num > 0)
                        {
                            item = items[num - 1];
                            if ((item.Style == ToolBarItemStyle.Separator) && flag3)
                            {
                                flag = false;
                            }
                        }
                        if (flag)
                        {
                            items.Add(item2);
                        }
                        flag = true;
                    }
                }
            }
            int count = items.Count;
            if (count > 0)
            {
                item = items[count - 1];
                if (item.Style == ToolBarItemStyle.Separator)
                {
                    items.RemoveAt(count - 1);
                }
            }
            this.chevronMenu.Items = items;
            this.chevronMenu.Style = VisualStyle.IDE;
            this.chevronMenu.TrackPopup(control.PointToScreen(point));
            if (flag2)
            {
                this.ToolbarSizeChanged();
                for (num2 = 0; num2 < this.items.Count; num2++)
                {
                    item2 = this.items[num2];
                    if (item2.Style == ToolBarItemStyle.ComboBox)
                    {
                        WindowsAPI.SetParent(item2.ComboBox.Handle, base.Handle);
                        ComboBoxBase comboBox = (ComboBoxBase) item2.ComboBox;
                        comboBox.ToolBarUse = true;
                        this.UpdateItem(num2);
                        comboBox.Invalidate();
                    }
                }
            }
        }

        public YidanSoft.Library.EditorUtility.CommandBars.BarType BarType
        {
            get
            {
                return this.barType;
            }
        }

        protected override System.Windows.Forms.CreateParams CreateParams
        {
            get
            {
                System.Windows.Forms.CreateParams createParams = base.CreateParams;
                createParams.ClassName = "ToolbarWindow32";
                createParams.ExStyle = 0;
                createParams.Style = 0x56000000;
                createParams.Style |= 0x4c;
                createParams.Style |= 0x8900;
                if (this.HasText())
                {
                    createParams.Style |= 0x1000;
                }
                return createParams;
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(1, 1);
            }
        }

        public ToolBarItemCollection Items
        {
            get
            {
                return this.items;
            }
        }

        public bool UseNewRow
        {
            get
            {
                return this.useNewRow;
            }
        }

        private enum State
        {
            None,
            Hot,
            HotTracking
        }
    }
}

